"use strict";

import axios from "axios";
import {Message} from "@element-plus/icons-vue";

const BASE_URL = './api';
const ADMIN_URL = '/admin';

let config = {
  baseURL: `${BASE_URL}${ADMIN_URL}`,
  timeout: 1000000, // Timeout
  withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function(response) {
    // Do something before request is sent
    if ( response.data.code === '2013' ) {
      Message.error( response.data.msg );
    }else if ( response.data.code  === '2' ) {
      sessionStorage.clear();
      localStorage.clear();
      window.location.herf="/";
      Message.error( response.data.msg );
      router.push({
        name:"login"
      });
    }else if ( response.data.code === '109700') {
      Message.error( '不支持的快递公司、单号为空、单号超长' );
    }else if ( response.data.code !== '0' ) {
      Message.error( response.data.msg );
    }
    return response;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    return response;
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function(Vue, options) {
  console.log(options)
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

// createApp(App).use(Plugin)

export default Plugin;
